package com.arron.ssm.utils;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Classname TestProxy
 * @Date 2020/8/29 21:11
 * @Created by 刘
 */
public class TestProxy {

    public static void main(String[] args) {

        //使用cglib创建动态代理
        //创建核心类
        Enhancer enhancer = new Enhancer();
        //创建哪种类型的代理
        enhancer.setSuperclass(App.class);

        //设置回调，相当于jdk中的invoke方法
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                methodProxy.invokeSuper(o,objects);
                return null;
            }
        });
        //创建app的代理对象
        App app = (App) enhancer.create();

        System.out.println(app.getClass());
    }

}
